﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;

namespace FrbaCommerce.Clases
{
    class Paginador
    {
        int cantItems;
        int cantPaginas;
        DataTable data;

        public Paginador (DataTable data, int cantItems ) 
        {
            this.data = data;
            this.cantItems = cantItems;
            this.cantPaginas = getCantidadDePaginas();
        }
  
        public int getCantidadDePaginas () 
        {
            int paginas = (int) data.Rows.Count / cantItems;
            if (data.Rows.Count % cantItems > 0) 
            {
                paginas++;
            }
            return paginas;
        }

        public DataTable getPagina (int p) 
        {
            DataTable pagina = new DataTable();
            pagina = this.data.Clone();

            int iItems = (p - 1) * cantItems;
            int fin = iItems;

            if (p == cantPaginas)
            {
                fin += (this.data.Rows.Count % this.cantItems) -1;
            }

            else
            {
                fin += cantItems-1;
            }

            for (; iItems <= fin; iItems++) 
            {
              pagina.ImportRow(this.data.Rows[iItems]);
            }

            return pagina;
        }

    }
}
